package cn.controller;

import cn.pojo.CloudPhoto;
import cn.service.CloudPhotoService;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;
import java.util.Map;

/**
 * 图片功能
 */
@CrossOrigin
@Controller
@RequestMapping("/photo")
public class CloudPhotoController {

    @Autowired
    private CloudPhotoService photoService;

    /**
     * 批量上传图片
     * 可以上传单个，也可以上传多个
     * @param
     * @return
     */
    @PostMapping("/insert")
    public Map<String,Object> addPhoto(@RequestParam(value = "photo") MultipartFile[] photoList,
                                       @RequestParam(value = "userId") String userId) throws Exception {
        return photoService.addPhoto(photoList,userId);
    }

    /**
     * 删除图片，修改图片的状态
     * @param photoId
     * @return
     */
    @PostMapping("/delete")
    public Boolean deletePhoto(Integer photoId){
        System.out.println(photoId);
        return photoService.deletePhoto(photoId);
    }

    /**
     * 搜索所有图片
     * @param userId
     * @return
     */
    @ResponseBody
    @GetMapping("/selectAllPhoto")
    public Map<String,List<CloudPhoto>> selectAllPhoto(Integer userId){
        System.out.println(userId);
        Map<String,List<CloudPhoto>> photoList = photoService.selectAllPhoto(userId);
        System.out.println(photoList);
        return photoService.selectAllPhoto(userId);
    }

    /**
     * 搜索回收站照片
     * @param userId
     * @return
     */
    @GetMapping("/selectDeletePhoto")
    public Map<String,List<CloudPhoto>> selectDeletePhoto(Integer userId){return photoService.selectDeletePhoto(userId);}

    /**
     * 添加图片到相册中
     * @param albumId
     * @param photoId
     * @return
     */
    @PostMapping("/insertIntoAlbum")
    public Map<String,Object> insertIntoAlbum(Integer albumId,Integer[] photoId){return photoService.insertIntoAlbum(albumId,photoId);}

    /**
     * 添加首页轮播图片，单上传
     * @param photo
     * @param userId
     * @return
     */
    @PostMapping("/insertFirstPhoto")
    public Map<String,Object> insertFirstPhoto(@RequestParam(value = "photoF") MultipartFile photo,
                                               @RequestParam(value = "userId") String userId) throws IOException, GitAPIException {
        return photoService.insertFirstPhoto(userId,photo);
    }

    @GetMapping("/selectFirstPhoto")
    public Map<String,List<CloudPhoto>> selectFirstPhoto(Integer userId){return photoService.selectFirstPhoto(userId);}

    @GetMapping("/downLoadPhoto")
    public Map<String,String> downLoadPhoto(List<String> photoUrls,String userId,@RequestParam(required = false) String filePath){return photoService.downLoadPhoto(photoUrls,userId,filePath);}

    @GetMapping("/d")
    public void download(String file, int userid, HttpServletResponse resp, HttpServletRequest req) throws IOException {
        File down = new File("E:/java/apache-tomcat-9.0.34/webapps/a/" + userid + "/" + file);

        ServletContext context = req.getServletContext();
        String mimeType = context.getMimeType(file);
        System.out.println(mimeType);
        if (mimeType != null && mimeType != "") {
            mimeType = "application/octet-stream";
        }

        resp.setContentType(mimeType);
        resp.setContentLength((int) down.length());
        resp.setHeader("Content-Disposition", String.format("attachment; filename=\"%s\"", down.getName()));

        OutputStream os = resp.getOutputStream();

        byte[] img = new byte[1024 * 1024 * 10];

        BufferedInputStream bis = null;
        // 读取filename
        bis = new BufferedInputStream(new FileInputStream(file));
        int i = bis.read(img);
        while (i != -1) {
            os.write(img, 0, img.length);
            os.flush();
            i = bis.read(img);
        }

        resp.flushBuffer();
    }

    @RequestMapping("/a")
    public String index(){
        return "index2";
    }

    @RequestMapping("/login")
    public String login(){return  "/index";}
}
